var hdmain = (function () {
	var sumData;
	
	sumData = {
        mainData:{},
        accountSum:null,
        endDate:null,
        daysLeft:null,
		init: function () {
            sumData.accountSum = $("#spAccountsSum");
            sumData.endDate = $("#spEndDate");
            sumData.daysLeft = $("#spDaysLeft");
            var monthNames = [ "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December" ];
    		$("#spMonthYearTitle").html(monthNames[(new Date()).getMonth()] + " " + (new Date()).getFullYear());
			sumData.updateData();
            $("#btnAccount").click(function(){
                hdaccounts.updateAccounts();
                $.mobile.changePage("#page6");
            });
            $("#btnChangeStartEndDate").click(function(){
                hdchangedate.updateStartEndDates();
                $.mobile.changePage("#page4");
            });
            $("#btnSavings").click(function(){
                hdsavings.updateSavings();
                $.mobile.changePage("#page8");
            });
            $("#btnReserved").click(function(){
                hdreserved.updateReserved();
                $.mobile.changePage("#page10");
            });
            $("#btnCurrentState").click(function(){
                hdcurrentstate.show();

            });
		},
		updateData: function () {
			var currentSum = 0, daysLeft = 0, reservedSum = 0;
			data.getReservedSum( function (sum){
				reservedSum = sum;
			});
            data.getAccountList( function (results){
                var i,num, sum = 0;
                if(results.rows.length !== 0) {
                    num = results.rows.length;
                    for(i=0; i<num; i+=1){
                        sum += parseInt(results.rows.item(i).Sum, 10)*100;
                    }
                    $(sumData.accountSum).html(addCommas(sum/100) + " RSD");
                    sumData.mainData.AccountSum = sum;
                }
            });
            data.getSumState(function (data){
                var tempSD, tempED, sd, ed, tempSum, updatedDates;
                updatedDates = UpdateDates(data.item(0).StartDate, data.item(0).EndDate)
                $(sumData.endDate).html(updatedDates.endDate);
                $(sumData.daysLeft).html("Days Left: " + updatedDates.daysBetween);
                daysLeft = updatedDates.daysBetween;
            	currentSum = data.item(0).Sum - reservedSum;
                $("#spCurrentState").html(addCommas(currentSum) + " RSD");
                $("#spDalySum").html("Daily sum: " + addCommas( parseInt((currentSum/daysLeft)*100)/100) + " RSD");
            });
            data.getSavingsList(function (results){
                var i, num, total=0;
                num = results.rows.length;
                if (num !== 0) {
                    for(i=0; i < num; i += 1){
                        total = ((total*100) + (results.rows.item(i).Sum * 100))/100;
                    }

                }
                $("#spSavingsSum").html(addCommas(total) + " RSD");
            });
            data.getReservedList(function (results){
                var i, num, total=0;
                num = results.rows.length;
                if (num !== 0) {
                    for(i=0; i < num; i += 1){
                        total = ((total*100) + (results.rows.item(i).Sum * 100))/100;
                    }

                }
                $("#spReservedSum").html(addCommas(total + " RSD"));
            });
            data.getTodayTakenData(function (data){
                if(data.item(0).Sum !== null){
                    var oldState = 0, dailySum = 0, tempCurrentSum;
                    //$("#spTakenToday").html(addCommas(data.item(0).Sum) + " RSD");
                    oldState = parseInt((data.item(0).Sum*100)+(currentSum*100))/100;
                    dailySum = parseInt((oldState/daysLeft)*100)/100;
                    if(data.item(0).Sum > dailySum){
	                    tempCurrentSum = roudNumber(oldState-(data.item(0).Sum - dailySum))
	                    dailySum = parseInt((tempCurrentSum/daysLeft)*100)/100;
	                }
                    $("#spDalySum").html("Daily sum: " + addCommas( dailySum) + " RSD");
                }else{
                    $("#spDalySum").html(addCommas( "Daily sum: " + parseInt((currentSum/daysLeft)*100)/100) + " RSD");
                }
            });
		}
	};
	this.updateData = function () {
		sumData.updateData();
	};
	this.init = function () {
        /*document.addEventListener("backbutton", function(e){
            if($.mobile.activePage.is('#page')){
                e.preventDefault();
                navigator.app.exitApp();
            }
            else {
                navigator.app.backHistory()
            }
        }, false);*/
		sumData.init();
	};
	
	this.init();
	return this;
}(hdmain || {}));